package com.example.chinawenddingexpo.repository;

import com.example.chinawenddingexpo.model.UserCoupon;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface UserCouponRepository extends JpaRepository<UserCoupon, Long> {
    
    List<UserCoupon> findByUserIdOrderByReceivedTimeDesc(Long userId);
    
    List<UserCoupon> findByUserIdAndStatusOrderByReceivedTimeDesc(Long userId, UserCoupon.UseStatus status);
    
    @Query("SELECT uc FROM UserCoupon uc WHERE uc.user.id = :userId AND uc.status = 'UNUSED' AND uc.coupon.endTime >= :now")
    List<UserCoupon> findUnusedCoupons(@Param("userId") Long userId, @Param("now") LocalDateTime now);
    
    UserCoupon findByUserIdAndCouponId(Long userId, Long couponId);
    
    @Query("SELECT uc FROM UserCoupon uc WHERE uc.coupon.exhibitor.id = :exhibitorId AND uc.status = 'USED'")
    List<UserCoupon> findUsedCouponsByExhibitor(@Param("exhibitorId") Long exhibitorId);
}